<?php
class setting extends model {

    public function get_host_id($host, $self, $https = 'off'){

        // Store
        if (($https == 'on') || ($https == '1')) {

            $host = 'https://' . str_replace('www.', '', $host);
            $host .= rtrim(dirname($self), '/.\\') . '/';

            $sql = "SELECT * FROM " . $this->db_prefix . "store 
                    WHERE REPLACE(https_server, 'www.', '') = :host";
        } else {
            $host = 'http://' . str_replace('www.', '', $host);
            $host .= rtrim(dirname($self), '/.\\') . '/';

            $sql = "SELECT * FROM " . $this->db_prefix . "store 
                    WHERE REPLACE(http_server, 'www.', '') = :host";
        }

        $query = $this->pdo->prepare($sql);

        $query->execute(array(':host' => $host));

        if ($query->rowCount()) {
            $result = $query->fetch(PDO::FETCH_ASSOC);
            return $result['store_id'];
        }
        
        return 0; 
    }

    public function load($store_id, $default = true){

        // Settings
        if($default){
            $sql = "SELECT * FROM " . $this->db_prefix . "setting 
                WHERE store_id = '0' 
                OR store_id = :store_id
                ORDER BY store_id ASC";
        }else{
            $sql = "SELECT * FROM " . $this->db_prefix . "setting 
                WHERE store_id = :store_id
                ORDER BY store_id ASC";
        }

        $query = $this->pdo->prepare($sql);

        $query->execute(array(':store_id' => $store_id));

        $settings = $query->fetchAll(PDO::FETCH_ASSOC); 

        $result = array();
 
        foreach($settings as $setting) {
            if (!$setting['serialized']) {
                $result[$setting['s_key']] = $setting['s_value'];
            } else {
                $result[$setting['s_key']] = unserialize($setting['s_value']);
            }
        }

        return $result;
    }
}
?>
